
//
//  MTQuestionAnswerVC.m
//  met
//
//  Created by 李晓俊 on 2019/5/20.
//  Copyright © 2019 李晓俊. All rights reserved.
//

#import "MTQuestionHelpVC.h"
#import "MTQuestionAnswerView.h"
#import "XJTypeWriterTextView.h"
#import "CLChocieAnswerButton.h"

#define kChoiceButtonTag  2000

@interface MTQuestionHelpVC ()
@property (nonatomic, strong) UIButton *lookBtn;
@property (nonatomic, strong) UIButton *doneBtn;

@property (nonatomic,strong) UIView *backgroundView;
@property (nonatomic,strong) UIImageView *backgroundImage;

@property (nonatomic,strong) UIImageView *leftImage;
@property (nonatomic,strong) UIImageView *rightImage;
//问题视图
@property (nonatomic,strong) MTQuestionAnswerView *questionView;
//问题文字框
@property (nonatomic,strong) XJTypeWriterTextView *questionTextView;
//选择框
@property (nonatomic,strong) UIView * choiceView;

@property (nonatomic,strong) NSString *isSuccess;
@property (nonatomic,strong) NSString *isUser;
@property (nonatomic,strong) NSString *isEnd;
@property (nonatomic,assign) NSInteger qCount;

@property (nonatomic,strong) NSMutableArray *dataArr;

@end

@implementation MTQuestionHelpVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"漂流卡";
    //初始化
    self.isSuccess = @"";
    self.isUser = @"";
    self.isEnd = @"";
    self.qCount = 1;
    self.dataArr = [NSMutableArray new];
    
    NSMutableDictionary *dic = [NSMutableDictionary new];
    dic[@"question"] = @"你喜欢猫么？";
    dic[@"answer1"] = @"喜欢";
    dic[@"answer2"] = @"不喜欢";
    dic[@"answerfail"] = @"讨厌不喜欢猫的人，不会和他们做朋友。";
    dic[@"isLike"] = @"1";
    [self.dataArr addObject:dic];
    self.choiceView.hidden = YES;
    
    _questionTextView.content = [dic objectForKey:@"question"];
//    @"你喜欢猫吗你喜欢猫吗你喜欢猫吗你喜欢猫吗你喜欢猫吗你喜欢猫吗你喜欢猫吗";
    [_questionTextView startTypewrite];
    
    _questionView.name.text = KUSER.nickName;
    _questionView.rightName.text = @"TA";

}

- (void)CreateNav {
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [WRNavigationBar wr_setDefaultNavBarShadowImageHidden:YES];
}
- (void)initUI{
 
    [self.view addSubview:self.backgroundView];
    [self.backgroundView addSubview:self.leftImage];
    [self.backgroundView addSubview:self.rightImage];
    [self.leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(0);
        make.width.mas_equalTo(244);
        make.height.mas_equalTo(456);
    }];
    [self.rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(0);
        make.width.mas_equalTo(214);
        make.height.mas_equalTo(436);
    }];
    self.leftImage.hidden = NO;
    self.rightImage.hidden = YES;
    
    [self.view addSubview:self.lookBtn];
    [self.view addSubview:self.doneBtn];
    self.doneBtn.hidden = YES;
    
    [self.backgroundView addSubview:self.questionView];
    [self.view addSubview:self.choiceView];
    self.choiceView.hidden = YES;

}
- (UIButton *)lookBtn{
    if (_lookBtn == nil) {
        CGRect rect = CGRectMake(12, kScreenHeight - 59 - 12 - BottomHeight, kScreenWidth - 24 , 59);
        _lookBtn = [[UIButton alloc] initWithFrame:rect text:@"赞TA的卡牌" textcolor:HEXCOLOR(0x333333) backgroundcolor:HEXCOLOR(0xFEF45C) fontSize:26 haveCorner:NO];
        _lookBtn.titleLabel.font = CLRudeCircleTextFont(18);
        _lookBtn.layer.cornerRadius = 10;
        _lookBtn.layer.masksToBounds = YES;
        _lookBtn.layer.borderWidth = 2;
        _lookBtn.layer.borderColor = KBlackColor.CGColor;
        [_lookBtn addTarget:self action:@selector(lookClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _lookBtn;
}
- (UIButton *)doneBtn{
    if (_doneBtn == nil) {
        CGRect rect =  CGRectMake(12, kScreenHeight - 59 - 12 - BottomHeight, kScreenWidth - 24 , 59);
//        CGRectMake(kScreenWidth -12-kFit(90), kScreenHeight - 59 - 12 - BottomHeight, kFit(90) , 59);
        _doneBtn = [[UIButton alloc] initWithFrame:rect text:@"进入私聊" textcolor:HEXCOLOR(0x333333) backgroundcolor:HEXCOLOR(0xFEF45C) fontSize:26 haveCorner:NO];
        _doneBtn.titleLabel.font = CLRudeCircleTextFont(18);
        _doneBtn.layer.cornerRadius = 10;
        _doneBtn.layer.masksToBounds = YES;
        _doneBtn.layer.borderWidth = 2;
        _doneBtn.layer.borderColor = KBlackColor.CGColor;
        [_doneBtn addTarget:self action:@selector(doneClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _doneBtn;
}
- (UIView *)backgroundView{
    if (_backgroundView == nil) {
        CGRect rect = CGRectMake(12, NavHeight, kScreenWidth - 24  , kScreenHeight - 59 - 22 - BottomHeight - NavHeight);
        _backgroundView = [[UIView alloc] initWithFrame:rect];
        _backgroundView.layer.cornerRadius = 10;
        _backgroundView.layer.masksToBounds = YES;
        _backgroundView.backgroundColor = KRedColor;
        
        self.backgroundImage = [UIImageView new];
        [_backgroundView addSubview:self.backgroundImage];
        [self.backgroundImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.right.mas_equalTo(0);
        }];
        self.backgroundImage.image = [UIImage imageNamed:@"背景图"];
    }
    return _backgroundView;
}
- (UIImageView *)leftImage{
    if (_leftImage == nil) {
        _leftImage = [UIImageView new];
        _leftImage.image = [UIImage imageNamed:@"默认女"];
    }
    return _leftImage;
}
- (UIImageView *)rightImage{
    if (_rightImage == nil) {
        _rightImage = [UIImageView new];
        _rightImage.image = [UIImage imageNamed:@"默认男"];
    }
    return _rightImage;
}
- (MTQuestionAnswerView *)questionView{
    if (_questionView == nil) {
        _questionView = [[MTQuestionAnswerView alloc] initWithFrame:CGRectMake(0, self.backgroundView.height - 150, kScreenWidth - 24,150 )];
        
        _questionTextView = [[XJTypeWriterTextView alloc] initWithFrame:CGRectMake(20, 30, kScreenWidth-20*2-24, 100)];
        _questionTextView.backgroundColor = [UIColor clearColor];
        _questionTextView.font = CLRudeCircleTextFont(18);
        _questionTextView.hasSound = NO;
        _questionTextView.typewriteEffectColor = KWhiteColor;
        _questionTextView.textColor = KWhiteColor;
        _questionTextView.textAlignment = NSTextAlignmentCenter;
        _questionTextView.typewriteTimeInterval = 0.1;
        _questionTextView.editable = NO;
        
        _questionTextView.typewriteEffectBlock = ^(){
            if ([self.isUser isEqualToString:@"1"]) {
                self.isUser = @"";
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self.rightImage.hidden = YES;
                    self.leftImage.hidden = NO;
                    _questionView.rightName.hidden = YES;
                    _questionView.name.hidden = NO;
                    if ([self.isEnd isEqualToString:@"1"]) {
                        if ([self.isSuccess isEqualToString:@"1"]) {
                            //成功
                            self.doneBtn.hidden = NO;
//                            self.lookBtn.width = kScreenWidth - 24 - kFit(90) - 10;
                            [self.doneBtn setTitle:@"进入私聊" forState:UIControlStateNormal];
                            _questionView.name.text = @"GET!";
                            _questionView.name.textColor = KMainColor;
                            _questionTextView.content = @"已成功添加对方！快去撩TA吧";
                            [_questionTextView startTypewrite];
                        }else {
                            //失败
                            self.doneBtn.hidden = NO;
//                            self.lookBtn.width = kScreenWidth - 24 - kFit(90) - 10;
                            _questionView.name.text = @"LOSE";
                            _questionView.name.textColor = KMainColor;
                            _questionTextView.content = @"讨厌不喜欢小动物的人，不会跟他们做朋友";
                            [_questionTextView startTypewrite];
                            
                            [self.doneBtn setTitle:@"GG" forState:UIControlStateNormal];
                        }
                    }else {
                        _questionTextView.content = @"你喜欢猫吗";
                        [_questionTextView startTypewrite];
                    }
                });
            }else {
                self.rightImage.hidden = YES;
                self.leftImage.hidden = NO;
                _questionView.rightName.hidden = YES;
                _questionView.name.hidden = NO;
                
                if ([self.isEnd isEqualToString:@"1"]) {
                    return ;
                }
                [self reloadAnswer];
                self.choiceView.hidden = NO;
            }
            
        };
        [_questionView addSubview:_questionTextView];
        
    }
    
    return _questionView;
}
- (UIView *)choiceView {
    if (_choiceView == nil) {
        _choiceView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight/2.0 - TabbarHeight  , kScreenWidth, 81*2)];
        _choiceView.backgroundColor = [UIColor clearColor];
        for (int i = 0; i < 2; i++) {
            CLChocieAnswerButton * choiceButton = [[CLChocieAnswerButton alloc] init];
            choiceButton.frame = CGRectMake(25, 14 + (65+10)*i, kScreenWidth - 50, 65);
            [_choiceView addSubview:choiceButton];
//            choiceButton.layer.borderWidth = 2;
            choiceButton.layer.cornerRadius = 4;
            choiceButton.layer.masksToBounds = YES;
            
//            choiceButton.layer.borderColor = KBlackColor.CGColor;
            choiceButton.tag = kChoiceButtonTag + i;
            [choiceButton addTarget:self action:@selector(clickChoiceButton:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    return _choiceView;
}
- (void)reloadAnswer {
    CLChocieAnswerButton *but = [_choiceView viewWithTag:kChoiceButtonTag + 1];
    if (but) {
        but.hidden = NO;
    }
    for (int i = 0; i < 2; i++) {
        CLChocieAnswerButton * button = [_choiceView viewWithTag:kChoiceButtonTag + i];
        NSDictionary *dic = [self.dataArr objectAtIndex:0];
        if (i == 0) {
            [button setTextToButton:[dic objectForKey:@"answer1"]];
        } else if (i == 1) {
            [button setTextToButton:[dic objectForKey:@"answer2"]];
        }
    }
}
- (void)clickChoiceButton:(CLChocieAnswerButton *) sender {
    NSDictionary *dic = [self.dataArr objectAtIndex:0];
    self.rightImage.hidden = NO;
    self.leftImage.hidden = YES;
    _questionView.rightName.hidden = NO;
    _questionView.name.hidden = YES;
    switch (sender.tag - kChoiceButtonTag) {
        case 0:
        {
            if (self.qCount == self.dataArr.count) {
                self.isEnd = @"1";
            }
            self.qCount ++;
            self.choiceView.hidden = YES;
            self.isUser = @"1";
            self.isSuccess = @"1";

            _questionTextView.content = [dic objectForKey:@"answer1"];
            [_questionTextView startTypewrite];
            
        }
            break;
        case 1:
        {
            if (self.qCount == self.dataArr.count) {
                self.isEnd = @"1";
            }
            self.qCount ++;
            self.choiceView.hidden = YES;
            self.isUser = @"1";
            self.isSuccess = @"0";
            _questionTextView.content = [dic objectForKey:@"answer2"];
            [_questionTextView startTypewrite];
        }
            break;
        case 2:
        {
//            if (self.qCount == self.dataArr.count) {
//                self.isEnd = @"1";
//            }
//            self.qCount ++;
//            //返回
//            self.isUser = @"1";
//            self.choiceView.hidden = YES;
//            //失败
//            self.isSuccess = @"0";
        }
            break;
        default:
            break;
    }
}
//赞
- (void)lookClick {
    [self.view maketishiToast:@"点赞成功"];
}

- (void)doneClick {
    if ([self.isSuccess isEqualToString:@"1"]) {
        //私聊
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
@end
